/**
 * @Title: settleAnalyseMain_ctrl.js
 * @Description: TODO
 * Copyright: Copyright (c) 2014 
 * Company:中国平安
 *
 * @Project HIMS-FWA
 * @author QIANDUO875
 * @date 2014年9月26日 上午10:32:01
 * @version V1.0
 */
;
(function(){
	'use strict';
	
	var app = angular.module('fwa.mainApp');
	app.controller('SettleAnalyseOrgCtrl',
	    ['$scope','SettleAnalyseOrgService','$stateParams', 'SettleAnalyseChartService',
	     '$state','SettleAnalyseService','Modal','BaseURL','CommunityService',
	     function($scope,SettleAnalyseOrgService,$stateParams,SettleAnalyseChartService,
	    		 $state,SettleAnalyseService,Modal,baseURL,CommunityService){
		
	    	/*********************************************************
			 * 
			 * Scope & Variables Init
			 * 
			 */
	    	var orgId = $stateParams.id,
	    		checkedArray = [],
	    		basicData = [];
	    	
	    	$scope.page = {
	    		limit:10
	    	}
	    	
	    	/*********************************************************
			 * 
			 * Private function
			 * 
			 */
	    	
	    	//查询
			function query(params) {
				params = $.extend({
					orgId:orgId,
					indicator:$stateParams.indicator,
					startDate:$scope.$parent.query.date.start,
					endDate:$scope.$parent.query.date.end,
					depart:$stateParams.dept.depart,
					start:1,
					limit:$scope.page.limit
				},params);
				SettleAnalyseOrgService.listOrgDepartments(params).
					success(function(res){
						angular.forEach(basicData,function(data,index){
							data.checked = false;
						});
						if (res.data) {
							var arr = basicData.concat(toFixedDatas(res.data));
							
							SettleAnalyseService.checkedInit(arr);
							
							$scope.tableData = arr;
							$scope.page.total = res.total;
						}
						else {
							$scope.page.total = 0;
							$scope.tableData = [];
						}
						
						
					});
				
			}
	    	
	    	// 导出
			$scope.onExport=function(){
				//判断日期跨度不能大于一年
				var checkTemp = CommunityService.checkDateSpan($scope.$parent.query.date.start,$scope.$parent.query.date.end);
				if(checkTemp!=''&& checkTemp.length>0){
					Modal.alert(checkTemp);
					return false;
				}
				var date=$scope.$parent.query.date;
				var startDate=date.start;
				var endDate=date.end;
				var indicator=$stateParams.indicator;
				var url=baseURL+"indication/orgExport.do" +
						"?indicator="+indicator+"&dataType="+SettleAnalyseService.typeSelected+
						"&startDate="+startDate+"&endDate="+endDate+"&orgId="+orgId;
			    window.location.href = url;
			}
	    	
	    	/*********************************************************
			 * 
			 * Event Handler
			 * 
			 */
	    	
	    	$scope.onRowCheck = function(data) {
	    		checkedArray = SettleAnalyseService.checkedArray;
	    		if(data.checked && checkedArray.length >= 2) {
	    			Modal.alert('勾选超过2个!');
	    			data.checked = false;
	    			return;
	    		}
	    		var index = checkedArray.indexOf(data);
	    		if(data.checked && index == -1) {
	    			checkedArray.push(data);
	    		} else {
	    			if(index != -1) {
	    				checkedArray.splice(index,1);
	    			}
	    		}
	    		$scope.$parent.$broadcast('settleAnalyse.rowCheckChange',checkedArray);
	    	}
	    	
			$scope.$on('settleAnalyse.searchQuery', function() {
				$scope.page.current = 1;
	    		query({
	    			start:1,
	    			depart:$scope.$parent.query.depart
	    		});
	    		getOrgTableBasicData();
	    		$scope.$parent.$broadcast('settleAnalyse.rowCheckChange',SettleAnalyseService.checkedArray);
	    	});
			
			$scope.$on('pageTool.skipPage', function(e,page){
				query({
	    			depart:$scope.$parent.query.depart,
	    			start:page
	    		});
			});
	    	
			$scope.$on('settleAnalyse.searchReset', function(){
	    		$scope.$parent.query.doctor = null;
	    		$scope.$parent.query.depart = null;
	    	});
			
//			$scope.$on('settleAnalyse.dataTypeChange', function(e, type){
//				$scope.page.current = 1;
//	    		query({
//	    			start:1,
//	    			depart:$scope.$parent.query.depart
//	    		});
//			});
			
			
			/*********************************************************
			 * 
			 * Init
			 * 
			 */
			//以万为单位
			function toFixedDatas(datas) {
				var newDatas = [];
				if ($scope.$parent
					&& $scope.$parent.query 
					&& $scope.$parent.query.indicator ) {
						var isFlag = false;
						angular.forEach($scope.$parent.query.indicators,function(indicator,index){
							if ($scope.$parent.query.indicator == indicator) {
								isFlag = true;
							}
						});
						newDatas = toNewDatas(datas,isFlag);
					}
				return newDatas;
			}
			
			function toNewDatas(datas,isFlag) {
				angular.forEach(datas,function(data,index){
					if (isFlag) {
						data.first = (parseFloat(data.first)/10000).toFixed(2);
						data.second = (parseFloat(data.second)/10000).toFixed(2);
						data.third = (parseFloat(data.third)/10000).toFixed(2);
						data.fourth = (parseFloat(data.fourth)/10000).toFixed(2);
						data.fifth = (parseFloat(data.fifth)/10000).toFixed(2);
						data.sixth = (parseFloat(data.sixth)/10000).toFixed(2);
						data.seventh = (parseFloat(data.seventh)/10000).toFixed(2);
						data.eighth = (parseFloat(data.eighth)/10000).toFixed(2);
						data.ninth = (parseFloat(data.ninth)/10000).toFixed(2);
						data.tenth = (parseFloat(data.tenth)/10000).toFixed(2);
						data.eleventh = (parseFloat(data.eleventh)/10000).toFixed(2);
						data.twelfth = (parseFloat(data.twelfth)/10000).toFixed(2);
					}
					else {
						data.first = parseFloat(data.first).toFixed(2);
						data.second = parseFloat(data.second).toFixed(2);
						data.third = parseFloat(data.third).toFixed(2);
						data.fourth = parseFloat(data.fourth).toFixed(2);
						data.fifth = parseFloat(data.fifth).toFixed(2);
						data.sixth = parseFloat(data.sixth).toFixed(2);
						data.seventh = parseFloat(data.seventh).toFixed(2);
						data.eighth = parseFloat(data.eighth).toFixed(2);
						data.ninth = parseFloat(data.ninth).toFixed(2);
						data.tenth = parseFloat(data.tenth).toFixed(2);
						data.eleventh = parseFloat(data.eleventh).toFixed(2);
						data.twelfth = parseFloat(data.twelfth).toFixed(2);
					}
					data.yoy = parseFloat(data.yoy).toFixed(2);
					data.mom = parseFloat(data.mom).toFixed(2);
				});
				return datas;
			}
			if (!$scope.$parent.query.orgLevel) {
				$scope.$parent.query.orgLevel = SettleAnalyseService.orgLevel;
			}
			
//			SettleAnalyseOrgService.getOrgInfo(orgId).
//				success(function(res){
//					$scope.$parent.query.orgLevel = res.level;
//					$scope.$parent.query.orgCategory = res.category;
//					
//					$scope.$parent.orgSelected = res;
//				});
			
//			if(!$scope.$parent.departs || $scope.$parent.departs.length ===0) {
//				SettleAnalyseService.listAllDeparts(orgId).
//					success(function(res){
//						$scope.$parent.departs = res;
//					});
//			}
			
			$scope.settleAnalyseService = SettleAnalyseService;
			
			$scope.$watchCollection('settleAnalyseService.typeSelected',function(v){
				if (v) {
					getOrgTableBasicData();
				}
	    	});
			//获取统筹区以及医疗机构数据
			function getOrgTableBasicData() {
				SettleAnalyseOrgService.getOrgTableBasicData($scope.$parent.query.indicator,orgId,$scope.$parent.query.date).
				success(function(res){
					if($scope.$parent.query.view  == 'org'){
						basicData = [];
						angular.forEach(res,function(data,index){
							if(data.type != 'region'){
								basicData.push(data);
							}
						});
					}else{
						basicData = res;
					}
					basicData = toFixedDatas(basicData);
					$scope.$parent.query.depart = $stateParams.dept;
					$scope.$parent.query.org = orgId;
					$scope.page.current = 1;
					query({
						start:1
					});
				});
			}
			
	}]);
	
})();